<?php
# autoHTML: the dynamic PHP5 library for fast website prototyping
# http://code.google.com/p/autohtml/
#
# Copyright (c) 2010, Shu Zong Chen
# http://freelanceDreams.com/
# All rights reserved. 
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met: 
# 
#  * Redistributions of source code must retain the above copyright notice, 
#    this list of conditions and the following disclaimer. 
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution. 
#  * Neither the name of autoHTML nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission. 
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

function AUTOHTML_exception_handler($exception){
	ob_end_flush;
	echo('Uncaught exception: ' . $exception->getMessage());
	die();
}

//This doesn't work in PHP4
set_exception_handler('AUTOHTML_exception_handler');

ini_set('mbstring.internal_encoding','UTF-8');
ini_set('mbstring.http_input','UTF-8');
ini_set('mbstring.http_output','UTF-8');
ini_set('error_reporting', E_ALL ^ E_NOTICE);

function path_join(){
	return implode(DIRECTORY_SEPARATOR, func_get_args());
}

function f_insertComma($array, $template='%text%', $comma=', ', $safe_mode=false){
	//Funky function to automate long MySQL queries.
	$a = count($array);
	if ($a<=0 || !is_array($array)){
		return '';
	}
	$a--;
	$b = 0;
	$text = '';
	foreach ($array as $key => $value){
		if ($safe_mode && !get_magic_quotes_gpc()){
//			$value = mysql_real_escape_string($value);
		}
		$text.=preg_replace('/%(.*?)%/e', '\$$1', $template);
		if ($b<$a){
			$text.=$comma;
		}
		$b++;
	}
	return $text;
}

function check_input($string, $against, $max=0, $min=0){
	$return = true;
	$errors = array();
	switch (substr($against, 0, 1)){
		case '_':
			switch (substr($against, 1)){
				case 'ASCII':	$preg = '/\A(\w*)\z/'; $note='ASCII characters only';	break;
				case 'INT':		$preg = '/\A(\d*)\z/'; $note='Numerals only';	break;
				case 'FLOAT': $preg = '/\A(\d*)\.?(\d*)\z/'; $note='Float only'; break;
				case 'EMAIL':	$preg = '/\A(([a-z0-9._-](\+[a-z0-9])*)+@[a-z0-9.-]+\.[a-z]{2,6})\z/'; $note='Please use standard email format (xxx@yyy.zzz)';		break;
				case 'URL':		$preg = '/\A\(.*)\z/';	break;
				case 'VARCHAR':
				case 'DEFAULT':
				default:		$preg = '/.*/';			break;
			}
			break;
		default:
			$preg = $against;
			break;
	}
	if ($max==0){
		//only check min length
		if (strlen($string) < $min){
			array_push($errors, 'Value too short');
			$return = false;
		}
	}else{
		if (!(strlen($string) >= $min && strlen($string) <= $max)){
			array_push($errors, 'Value not within limits');
			$return = false;
		}
	}
	if (preg_match($preg, $string)){
	}else{
		array_push($errors, 'Invalid Input. ' . $note);
		$return = false;
	}
	return array('answer' => $return , 'error' => $errors);
}

function f_file2String($target){
	// Loads from to string
	if (function_exists('file_get_contents')){
		if (file_exists($target)){
			return @file_get_contents($target);
		}else{
			return "Could not open [$target] for reading.\n";
		}
	}else{
		return 'file_get_contents() not defined... use other function here';
	}
}

function f_loadFile($target, $safety=false){
	//$parse == true will include() the file
	//$safety_off == true will pass $target through makeURLsafe()
	if ($safety==true){
		$target = f_makeURLsafe($target);
	}
	if ($target==false){
		echo 'Invalid file: ', $target;
	}
	include($target);
	return true;
}

function f_makeURLsafe($string, $allowed){
	if (preg_match('/(\.\.)|(\/\/)/', $string)){
		return false;
	}
	$path_info = pathinfo($string);
	if (!in_array($path_info['extension'], $allowed)){
		return false;
	}else{
		return $string;
	}
}